<?php

namespace app\first\controller;

use aiplat\SDK\API;
use aiplat\SDK\Configer;
use Alipay\AopClient;
use Alipay\Request\AlipayUserInfoAuthRequest;
use Alipay\Request\AlipayUserInfoShareRequest;
use Alipay\Request\AlipayUserUserinfoShareRequest;
use app\first\model\ArticleInformation;
use app\first\model\ArticleLove;
use app\first\model\ArticleViewLog;
use app\first\model\Collection;
use app\first\model\DreamComments;
use app\first\model\DreamLove;
use app\first\model\Member;
use app\first\model\Message;
use app\first\model\Notice;
use app\first\model\RechargeList;
use app\first\model\Subscribe;
use app\first\model\Third;
use app\first\model\UsedMoney;
use app\first\model\UservipLog;
use app\first\validate\Article;
use app\first\validate\Member as MemberValidate;
use think\Db;
use think\db\Where;
use think\facade\Config;
use think\facade\Validate;

/**
 * Class User
 * @title 用户接口
 * @url /v1/user
 * @desc  有关于用户的接口
 * @route('v1/user')
 * @version 1.0
 */
class User extends Base{
    protected $autoWriteTimestamp = true;
    //是否开启授权认证
    public $apiAuth = true;
    //附加方法
    protected $extraActionList = [];

    //跳过鉴权的方法
    protected $skipAuthActionList = [
        'send_code',
        'register',
        'password',
        'login',
        'check_code',
        'charter',
        'text_trans',
        'find_password',
        'params',
        'third'
    ];

    public function __construct(){
        parent::__construct();
    }

    /**
     * @title 用户登录
     * @method login
     * @param string $account 账号 true
     * @param string $code 验证码 true
     * @param string $client_id 个推ID true
     * @route('v1/user/login','post')
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login($account='',$code='',$client_id=''){
        $valid = new MemberValidate();
        $_data = [
            'account'=>$account,
            'code'=> $code
        ];
        if(!$valid->scene('login')->check($_data)){
            return $this->sendError(0,$valid->getError());
        }
        $member = new Member;
        $_member = $member::where('account',$account)->find();
        Db::startTrans();
        $result = $result1 = true;
        if(!$_member){
            $black_list = Config::get('black_list.');
            if(!in_array($account,$black_list)){
                $data = $this->check_verify($code,true,$out);
                if(!$data){
                    Db::rollback();
                    return $this->sendError(0,$out);
                }
            }
            $save_data['nickname'] = '洁迅用户';
            $save_data['account'] = $account;
            $save_data['avatar']= '';
            $save_data['create_time']=time();
            $save_data['client_id']=$client_id;
            $id = $member->insertGetId($save_data);
            if(!$id){
                return $this->sendError(0,'注册失败');
            }
            $content = "欢迎您：{$save_data['account']}加入【{$this->site['title']}】大家庭！";
            $result = (new Notice())->addNew($id,'用户注册',$content,1);
            $push = new \app\admin\controller\Push();
            $payload = [
                'title'=>'用户注册',
                'content'=>$content,
                'payload'=>[
                    'type'=>1,
                    'id'=>$id,
                    'cid'=>$id
                ]
            ];
            $result1 = $push->pushOne($client_id,$payload,true);
        }else{
            $id = $_member['id'];
        }
        $_member1 = $member::getMember($id);
        $ip = request()->ip();
        $address = get_ip_address($ip);
        $token = self::jwt($_member1,'',$o);
        $_member1->access_token = $token;
        $_member1->last_login_time = time();
        $_member1->last_login_address = $address;
        $_member1->last_login_ip = $ip;
        $_member1->access_token_expire_time = $o['exp'];
        $_member1->client_id = $client_id;
        $result2 = $_member1->save();
        if($result && $result1 && $result2){
            Db::commit();
            $_member1 = $_member1->toArray();
            return $this->sendSuccess($_member1);
        }else{
            Db::rollback();
            return $this->sendError(0,'登陆失败');
        }
    }

    /**
     * @title 微信登陆
     * @method third
     * @param string $openid openid/seller_id true
     * @param string $nickname 微信昵称/支付宝昵称 true
     * @param string $avatar 微信头像/支付宝头像 true
     * @param string $client_id 个推id true
     * @param int $sex 类型 true 0男,1女
     * @param int $type 类型 true 1微信,2支付宝
     * @route('v1/user/third','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function third($openid='',$nickname='',$avatar='',$client_id='',$sex=0,$type=1){
        $valid = new MemberValidate();
        $_data['nickname']=$nickname;
        if($type==1){
            $_data['openid']=$openid;
        }else{
            $_data['seller_id']=$openid;
        }
        if(!$valid->scene('third')->check($_data)){
            return $this->sendError(0,$valid->getError());
        }
        $member = new Member;
        $_member = $member::where('openid',$openid)->find();
        Db::startTrans();
        $result = $result1 = true;
        if(!$_member){
            $save_data['nickname'] = '洁迅用户';
            $save_data['openid']= $openid;
            $save_data['avatar']= $avatar;
            $save_data['sex']=$sex;
            $save_data['create_time']=time();
            $save_data['client_id']=$client_id;
            $id = $member->insertGetId($save_data);
            if(!$id){
                return $this->sendError(0,'注册失败');
            }
            $content = "欢迎您：{$save_data['nickname']}加入【{$this->site['title']}】大家庭！";
            $result = (new Notice())->addNew($id,'用户注册',$content,1);
            $push = new \app\admin\controller\Push();
            $payload = [
                'title'=>'用户注册',
                'content'=>$content,
                'payload'=>[
                    'type'=>1,
                    'id'=>$id,
                    'cid'=>$id
                ]
            ];
            $result1 = $push->pushOne($client_id,$payload,true);
        }else{
            $id = $_member['id'];
        }
        $_member1 = $member::getMember($id);
        $ip = request()->ip();
        $address = get_ip_address($ip);
        $token = self::jwt($_member1,'',$o);
        $_member1->access_token = $token;
        $_member1->last_login_time = time();
        $_member1->last_login_address = $address;
        $_member1->last_login_ip = $ip;
        $_member1->access_token_expire_time = $o['exp'];
        $_member1->client_id = $client_id;
        $result2 = $_member1->save();
        if($result && $result1 && $result2){
            Db::commit();
            $count = (new Third())->where('avatar','eq',$openid)->count('1');
            if(!$count){
                (new Third())->save([
                    'tid'=>$openid,
                    'nickname'=>$nickname,
                    'avatar'=>$avatar,
                    'sex'=>$sex,
                    'create_time'=>time(),
                ]);
            }
            $_member1 = $_member1->toArray();
            return $this->sendSuccess($_member1);
        }else{
            Db::rollback();
            return $this->sendError(0,'登陆失败');
        }
    }

    /**
     * @title 绑定微信
     * @method bind_wechat
     * @param string $openid openid/支付宝 true
     * @param string $avatar 头像 true
     * @param string $nickname 昵称 false
     * @param int $sex 性别 false 0 0男,1女
     * @param int $type 类型 false 1 1微信,2支付宝
     * @route('v1/user/bind_wechat','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function bind_wechat($openid='',$avatar='',$nickname='',$sex=1,$type=1){
        $member = (new Member())->find(self::$uid);
        if(!$member){
            return $this->sendError(0,'用户不存在');
        }
        if($type==1){
            $member->openid=$openid;
        }else{
            $member->seller_id=$openid;
        }
        $member->avatar=$member['avatar']?$member['avatar']:$avatar;
        $member->nickname=$member['nickname']?$member['nickname']:$nickname;
        $member->sex=$sex;
        if(!$member->save()){
            return $this->sendError(0,'绑定失败');
        }
        $count = (new Third())->where('avatar','eq',$openid)->count('1');
        if(!$count){
            (new Third())->save([
                'tid'=>$openid,
                'nickname'=>$nickname,
                'avatar'=>$avatar,
                'sex'=>$sex,
                'create_time'=>time(),
            ]);
        }
        return $this->sendSuccess('','绑定成功');
    }

    /**
     * @title 检测登陆
     * @method check_login
     * @route('v1/user/check_login','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function check_login(){
        $info = (new Member())->find(self::$uid);
        if(!$info){
            return $this->sendError(0,'登陆已失效');
        }
        return $this->sendSuccess('','正常登陆');
    }

    /**
     * @title 用户登出
     * @method logout
     * @return object data
     * @route('v1/user/logout','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function logout(){
        $info = (new Member())->find(self::$uid);
        $info->access_token = '';
        $info->access_token_expire_time = 0;
        $info->client_id= '';
        if(!$info->save()){
            return $this->sendError(0,'登出失败');
        }
        return $this->sendSuccess('','登出成功');
    }

    /**
     * @title 刷新token
     * @method refresh_token
     * @route('v1/user/refresh_token','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function refresh_token(){
        $info = (new Member())->find(self::$uid);
        $token = self::jwt($info,'',$o);
        $ip = request()->ip();
        $address = get_ip_address($ip);
        $info->last_login_time = time();
        $info->last_login_address = $address;
        $info->last_login_ip = $ip;
        $info->access_token = $token;
        $info->access_token_expire_time = $o['exp'];
        $info->save();
        $data = [
            'access_token'=>$token,
            'access_token_expire_time'=>$o['exp']
        ];
        return $this->sendSuccess($data,'刷新成功');
    }

    /**
     * @title 修改用户信息
     * @method save
     * @param string $nickname 昵称 true
     * @param string $information 介绍 true
     * @param int $sex 性别 true 1 1男,2女,0未知
     * @param string $avatar 头像 true  头像地址
     * @route('v1/user/save','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function save($nickname='',$information='',$sex=1,$avatar=''){
        $data = request()->param();
        $member = new Member();
        $valid = new MemberValidate();
        if(!$valid->scene('edit')->check($data)){
            return $this->sendError(0,$valid->getError());
        }
        $info = $member->find(self::$uid);
        $info->nickname = $nickname;
        $info->information = $information;
        $info->sex = $sex;
        $info->avatar = $avatar;
        if(!$info->save()){
            return $this->sendError(0,'修改失败');
        }
        $time = date('Y-m-d H:i',time());
        $content = "您在{$time}修改了个人信息。";
        (new Notice())->addNew(self::$uid,'修改个人信息',$content,1);
        return $this->sendSuccess('','修改成功');
    }

    /**
     * @title 发送验证码
     * @method send_code
     * @param string $to 发送账号 true
     * @route('v1/user/send_code','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \Exception
     */
    public function send_code($to=''){
        if(!$to){
            return $this->sendError(0,lang('empty',[lang('receive')]));
        }
        if(strstr($to,'@')===false){
            if(!Validate::is($to,'/^1[34578]\d{9}$/')){
                return $this->sendError(0,'错误的手机号');
            }
            $arr = send_sms($to,'495073');
            if($arr['status']==1){
                return $this->sendSuccess('','发送成功');
            }else{
                return $this->sendError(0,'发送成功');
            }
        }else{
            if(!Validate::is($to,'email')){
                return $this->sendError(0,'错误的邮箱');
            }
            $_code = no_random(0,9,6);
            $html = "【".$this->site['title']."】:您本次的验证码:".
                $_code.",有效时间为5分钟.如果您没有使用【".$this->site['title']
                ."】相关产品,请自动忽略此邮件谢谢:)";
            if(!think_send_mail($to, $to,"【".$this->site['title']."】",$html)){
                return $this->sendError(0,'发送失败');
            }
            cookie($_code.'_session_code', $_code, 60 * 5);
            return $this->sendSuccess('','发送成功');
        }
    }

    /**
     * @title 检查验证码是否正确
     * @method check_code
     * @param string $code 验证码 true
     * @route('v1/user/check_code','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml*
     */
    public function check_code($code=''){
        if(!$code){
            return $this->sendError(0,'请填写验证码');
        }
        $data = $this->check_verify($code,false,$out);
        if(!$data){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','验证码正确');
    }
    /*
     * @title 修改手机号
     * @method phone
     * @param string $phone 手机号 true
     * @return Object User 用户信息
     * @route('v1/user/phone','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
    public function phone($phone=''){
        if(!$phone){
            return $this->sendError(0,'请输入手机号');
        }
        $member = Member::get(self::$uid);
        if(!$member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $member->tel = $phone;
        if(!$member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('success',[lang('upgrade')]));
    }
     */

    /**
     * @title 修改头像
     * @method avatar
     * @param string $avatar 头像地址 true
     * @return Object User 用户信息
     * @route('v1/user/avatar','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function avatar($avatar=''){
        $member = new Member;
        $m  = $member::getMember(self::$uid);
        if(!$m){
            return $this->sendError(0,'用户不存在');
        }
        $m->avatar = $avatar;
        if(!$m->save()){
            return $this->sendError(0,'修改失败');
        }
        $time = date('Y-m-d H:i',time());
        $content = "您在{$time}修改了头像。";
        (new Notice())->addNew(self::$uid,'修改头像',$content,1);
        $_m = $member::getMember(self::$uid);
        return $this->sendSuccess($_m);
    }


    /**
     * @title 设置签名
     * @method info
     * @param string $info 个性签名 true
     * @return Object 用户信息
     * @route('v1/user/info','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function info($info=''){
        if(!request()->isPost()){
            return $this->sendError(0,'请填写签名');
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('info')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,'用户不存在');
        }
        $_member->information=$info;
        if(!$_member->save()){
            return $this->sendError(0,'操作成功');
        }
        $time = date('Y-m-d H:i',time());
        $content = "您在{$time}修改了个人签名。";
        (new Notice())->addNew(self::$uid,'修改签名',$content,1);
        $_m = $member::getMember(self::$uid);
        return $this->sendSuccess($_m);
    }

    /**
     * @title 获取用户信息
     * @method profile
     * @route('v1/user/profile','get')
     * @return object data
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function profile(){
        $member = new Member;
        $info = $member::getMember(self::$uid);
        return $this->sendSuccess($info);
    }

    /**
     * @title 修改密码
     * @method password
     * @param string $password 密码 true
     * @param string $password1 手机号 true
     * @route('v1/user/password','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function password($password='',$password1=''){
        if(!$password1){
            return $this->sendError(0,'请填写确认密码');
        }
        if(!$password){
            return $this->sendError(0,'请输入密码');
        }
        if($password == $password1){
            return $this->sendError(0, '两次不一致');
        }
        $member = new Member;
        $_member = $member->where('id','eq',self::$uid)->find();
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->password = $this->get_password($password,0);
        if(!$_member->save()){
            return $this->sendError(0,'修改失败');
        }
        return $this->sendSuccess('','修改成功');
    }

    /**
     * @title 找回密码
     * @method find_password
     * @param string $phone 手机号 true
     * @param string $password 密码 true
     * @param string $password1 确认密码 true
     * @route('v1/user/find_password','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function find_password($phone='',$password='',$password1=''){
        if(!$phone){
            return $this->sendError(0,'请填写手机号');
        }
        if(!$password){
            return $this->sendError(0,'请输入密码');
        }
        if(!$password1){
            return $this->sendError(0,'请填写确认密码');
        }
        if($password == $password1){
            return $this->sendError(0, '两次不一致');
        }
        $member = new Member;
        $_member = $member->where('account','eq',$phone)->find();
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->password = $this->get_password($password,0);
        if(!$_member->save()){
            return $this->sendError(0,'修改失败');
        }
        return $this->sendSuccess('','修改成功');
    }

    /**
     * * @title 用户注册
     * @param string $account 手机号 true
     * @param string $code 验证码 true
     * @return Object User  注册用户
     * @route('v1/user/register','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function register($account='',$code=''){
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->check($data)){
            return $this->sendError(0,$validate->getError());
        }
//        $appkey = Config::get('rong.appKey');
//        $appSecret = Config::get('rong.appSecret');
//        $ro = new RongCloud($appkey,$appSecret);
        $member = new Member;
        if($member->where('account','eq',$account)->count()){
            return $this->sendError(0,'已经注册了');
        }
        $_data = $this->check_code($code);
        if($_data['status']==0){
            return $this->sendError(0,'验证码错误或已失效');
        }
        $data['nickname'] = '洁迅用户';
        $data['avatar']= '';
//        $data['password'] = $this->get_password($data['password'],0);
        $data['create_time']=time();
        $id = $member->insertGetId($data);
        if(!$id){
            return $this->sendError(0,'注册失败');
        }
//        $token = $ro->user()->getToken($id,$data['account'],'http://www.rongcloud.cn/images/logo.png');
//        $token = json_decode($token,true);
//        if($token['code']){
//            $member->where('id','eq',$id)->setField('rong_token',$token['token']);
//        }
        $notice = new Notice();
        $notice->addNew($id,'欢迎注册',"欢迎您：{$data['account']}加入洁讯大家庭！",1);
        $_member = $member::getMember($id);
        return $this->sendSuccess(['status'=>1,'message'=>'注册成功','data'=>$_member]);
    }

    /**
     * @title 修改昵称
     * @method nickname
     * @param string $nickname 昵称 true
     * @route('v1/user/nickname','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function nickname($nickname=''){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('nickname')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->nickname = $nickname;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        $time = date('Y-m-d H:i',time());
        $content = "您在{$time}修改了昵称。";
        (new Notice())->addNew(self::$uid,'修改昵称',$content,1);
        return $this->sendSuccess('',lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     * @title 修改性别
     * @method sex
     * @param int $sex 性别 true 1 0未知,1男,2女
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/sex','post')
     */
    public function sex($sex=-1){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('sex')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->sex = $sex;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     * @title 发布文章
     * @method publish
     * @param string $title 主题 true
     * @param string $description 介绍 false
     * @param string $content 内容 true
     * @param string $location 坐标 true
     * @param string $address 地址 true
     * @param string $city_id 城市ID true
     * @param string $cid_type 发布类型 true 1 参见v1/user/params
     * @param int $duration 显示天数 false
     * @param string $type 显示天数 true 'zhaopin'
     * @route('v1/user/publish','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function publish($cid_type=1,$type='zhaopin'){
        $data = request()->post();
        $params = Config::get('param.')[$cid_type];
        $_params = [];
        $rule['aid'] = 'require';
        $_rule = ['first_cate_id','twice_cate_id'];
        foreach ($params['params'] as $k => $v){
            /*if(strpos($k,'_cate_id') === false){
                if($cid_type==1){
                    if($v['require'] && $v['type']==$type){
                        $rule[$k] = 'require';
                    }
                    $tmp = isset($data[$k])?$data[$k]:'';
                    $_params[$k] = $tmp;
                }else{
                    if($v['require'] && $k != 'cover'){
                        $rule[$k] = 'require';
                        $tmp = isset($data[$k])?$data[$k]:'';
                        $_params[$k] = $tmp;
                    }
                }
                unset($data[$k]);
            }else{
                $data[$k] = isset($data[$k])?$data[$k]:'';
            }*/
            if(!in_array($k,$_rule)){
                if($cid_type==1){
                    $_type = $v['type']??'';
                    if($v['require'] && ($_type && $v['type']==$type)){
                        $rule[$k] = 'require';
                    }
                    $_params[$k] = isset($data[$k])?$data[$k]:'';
                }else{
                    if($v['require'] && $k != 'cover'){
                        $rule[$k] = 'require';
                    }
                    $_params[$k] = isset($data[$k])?$data[$k]:'';
                }
                unset($data[$k]);
            }else{
                $data[$k] = isset($data[$k])?$data[$k]:'';
            }

        }
        $data['mid'] = self::$uid;
        $data['create_time'] = time();
        $data['vip_type'] = self::$user['vip_type'];
        $valid = new Article();
        if(!$valid->check($data)){
            return $this->sendError(0,$valid->getError());
        }

        unset($data['type']);
//        p($data);
//        p($_params);die;
        Db::startTrans();
        $model = new \app\first\model\Article();
        $info = new ArticleInformation();
        $id = $model->insertGetId($data);
        $_params['aid'] = $id;
        $validate = Validate::make($rule);
        if(!$validate->check($_params)){
            return $this->sendError(0,$validate->getError());
        }
        $result = $info->save($_params);
        if($id && $result){
            Db::commit();
            return $this->sendSuccess('','发布成功');
        }else{
            Db::rollback();
            return $this->sendError(0,'发布失败');
        }
    }

    /**
     * @title 修改文章
     * @method upgrade
     * @param int $id 文章id true
     * @param string $title 主题 true
     * @param string $description 介绍 false
     * @param string $content 内容 true
     * @param string $location 坐标 true
     * @param string $address 地址 true
     * @param string $city_id 城市ID true
     * @param string $cid_type 发布类型 true 2 参见v1/user/params
     * @param int $duration 显示天数 false
     * @param int $type 类型 false zhaopin
     * @route('v1/user/publish','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function upgrade($id=0,$cid_type=1,$type='zhaopin'){
        $data = request()->post();
        $params = Config::get('param.')[$cid_type];
        $_params = [];
        $rule['aid'] = 'require';
        foreach ($params['params'] as $k => $v){
            if($cid_type == 1 && strpos($k,'_cate_id') === false){
                if($cid_type==1){
                    if($v['require'] && $v['type']==$type){
                        $rule[$k] = 'require';
                    }
                    $tmp = isset($data[$k])?$data[$k]:'';
                    $_params[$k] = $tmp;
                }else{
                    if($v['require'] && $k != 'cover'){
                        $rule[$k] = 'require';
                        $tmp = isset($data[$k])?$data[$k]:'';
                        $_params[$k] = $tmp;
                    }
                }
                unset($data[$k]);
            }else{
                $data[$k] = isset($data[$k])?$data[$k]:'';
            }
        }
        $data['mid'] = self::$uid;
        $data['create_time'] = time();
        $data['vip_type'] = self::$user['vip_type'];
        unset($data['type']);
        $valid = new Article();
        if(!$valid->check($data)){
            return $this->sendError(0,$valid->getError());
        }
        $model = new \app\first\model\Article();
        $info = new ArticleInformation();
        $article = $model->find($id);
        if(!$article){
            return $this->sendError(0,'内容不存在或已删除');
        }
        Db::startTrans();
        $model->save($data);
        $validate = Validate::make($rule);
        if(!$validate->check($_params)){
            return $this->sendError(0,$validate->getError());
        }
        if($_info = $info->where('aid','eq',$id)->find()){
            $result = $_info->isUpdate(true)->save($_params);
        }else{
            $_params['aid']=$id;
            $result = $info->save($_params);
        }
        if($id && $result){
            Db::commit();
            return $this->sendSuccess('','发布成功');
        }else{
            Db::rollback();
            return $this->sendError(0,'发布失败');
        }
    }

    /**
     * @title 我的发布
     * @method my_publish
     * @param int $cate_id 类型 true 0 0全部,1类目,2优惠,3资讯
     * @param int $limit 每页条数 true 10
     * @param int $page 当前页数 true 1
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     * @route('v1/user/my_publish','post')
     */
    public function my_publish($cate_id=0,$limit=10){
        $model = new \app\first\model\Article();
        $id = input('uid',self::$uid);
        $map[]=['mid','eq',$id];
        if($cate_id){
            array_push($map,['cid_type','eq',$cate_id]);
        }
        $list = $model->getList($map,'create_time desc',$limit);
        if(!$list){
            $list = [];
        }
        return $this->sendSuccess($list);
    }

    /**
     * @title 收藏
     * @method collection
     * @param int $aid 文章ID true
     * @param int $type 类型 true 1 1收藏,2取消收藏
     * @route('v1/user/collection','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collection($aid=0,$type=1){
        $collection = new Collection();
        $result = $collection->collection(self::$uid,$aid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 我的收藏
     * @method my_collection
     * @param int $cate_id 类型 true 0 0全部,1类目,2优惠,3资讯
     * @param int $limit 每页数量 true 10
     * @param int $page 当前页数 true 1
     * @route('v1/user/mycollection','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_collection($cate_id=0,$limit=10){
        $collection = new Collection();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        if($cate_id){
            array_push($map,['cate_type','eq',$cate_id]);
        }
        $result = $collection->getList($map,'create_time desc',$limit);
        return $this->sendSuccess($result);
    }

    /**
     * @title 关注
     * @method subscribe
     * @param int $uid 用户id true
     * @param int $type 类型 true 1 1收藏,2取消收藏
     * @route('v1/user/subscribe','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function subscribe($uid=0,$type=1){
        $collection = new Subscribe();
        $result = $collection->subscribe(self::$uid,$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 我的关注
     * @method my_subscribe
     * @param int $limit 每页数量 10 true
     * @param int $page 当前页数 1 true
     * @route('v1/user/mysubscribe','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_subscribe($limit=10,$page=1){
        $collection = new Subscribe();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        $result = $collection->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }

    /**
     * @title 腾讯闲聊机器人
     * @method charter
     * @param string $text 聊天内容 true
     * @route('v1/user/chart','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function charter($text=''){
        $config = Config::get('aiplat.');
        Configer::setAppInfo($config['app_id'],$config['app_key']);
        $params = [
            'time_stamp'=>time(),
            'nonce_str'=>self::secret(),
            'session'=>self::secret(6),
            'question'=>$text
        ];
        $response = API::nlp_textchat($params);
        return $this->sendSuccess($response);
    }

    /**
     * @title 翻译
     * @method text_trans
     * @param string $text 翻译内容 true  UTF-8编码
     * @param int $type 翻译类型 true 0 0自动识别（更多参考：https://ai.qq.com/doc/nlptrans.shtml）
     * @route('v1/user/texttrans','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function text_trans($text='',$type=0){
        $config = Config::get('aiplat.');
        Configer::setAppInfo($config['app_id'],$config['app_key']);
        $params = [
            'type'=>$type,
            'text'=> mb_convert_encoding($text, 'UTF-8','auto')
        ];
        $response = API::texttrans($params);
        return $this->sendSuccess($response);
    }

    /**
     * @title 我的消息
     * @method my_message
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_message','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_message($order='desc',$limit=10){
        $member = new Message();
//        $where[] = ['mid','eq',self::$uid];
        $list = $member->getList1([],"create_time {$order}",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 我的通知
     * @method my_notice
     * @param int $type 类型 true 1 类型:1账号相关,2发布相关,3充值相关,0全部
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_notice','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_notice($type=0,$order='desc',$limit=10){
        $notice = new Notice();
        $where[] = ['mid','eq',self::$uid];
        if($type){
            $where[] = ['type','eq',$type];
        }
        $list = $notice->getList1($where,"create_time {$order}",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 更新个推client_id
     * @method update_client_id
     * @param string $client_id 个推client_id true
     * @route('v1/user/update_client_id','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function update_client_id($client_id=''){
        $info = (new Member())->find(self::$uid);
        if(!$info){
            return $this->sendError(0,'用户存在在');
        }
        if(!$client_id){
            return $this->sendError(0,'请请填写client_id');
        }
        $info->client_id = $client_id;
        if(!$info->save()){
            return $this->sendError(0,'更新失败');
        }
        return $this->sendSuccess('','更新成功');
    }

    /**
     * @title 认证
     * @method certification
     * @param string $id_cards 身份证 true  多个使用','隔开
     * @param int $type 类型 true 1 1个人,2企业
     * @param string $licence 营业执照 true  多个使用','隔开
     * @param string $real_name 真实姓名 true
     * @param int $sex 性别 true 1 0未知,1男,2女
     * @param string $company_name 公司名 true
     * @param string $company_type 公司类型 true
     * @param string $company_business 经营范围 true
     * @route('v1/user/certification','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function certification($id_cards='',$type=1,$licence='',$real_name='',$sex=1,$company_name='',$company_type='',$company_business=''){
        $auth = new \app\first\model\Auth();
        $result = $auth->addNew(self::$uid,$id_cards,$type,$licence,$real_name,$sex,$company_name,$company_type,$company_business,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','申请成功');
    }

    /**
     * @title 我的认证
     * @method my_certification
     * @route('v1/user/my_certification','get')
     * @return object data 认证信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_certification(){
        $auth = new \app\first\model\Auth();
        $info = $auth->getOneById(self::$uid);
        return $this->sendSuccess($info, self::$uid);
    }

    /**
     * @title 获取消费金额
     * @method use_money
     * @route('v1/article/use_money','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @return title名称,mian_fei是否免费,bu_xian_ci免现次数0不限其他次数,price发布价格 data
     */
    public function use_money(){
        $m = new UsedMoney();
        $list = $m->select();
        return $this->sendSuccess($list);
    }

    /**
     * @title 获取额外参数
     * @method params
     * @param int $type 类型 false 0 0全部,1类目,2特惠,3快讯
     * @route('v1/artcle/params','get')
     * @return object data
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function params($type=0){
        $list = $type?Config::get('param.')[$type]:Config::get('param.');
        return $this->sendSuccess($list);
    }

    /**
     * @title 我的会员充值
     * @method my_recharge
     * @param int $type 类型 true 0 0全部,1未支付,1支付成功,2支付失败,-1取消
     * @param int $limit 条数 true 5
     * @param int $page 页数 true 1
     * @route('v1/user/my_recharge','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_recharge($type=0,$limit=5,$page=1){
        $recharge = new RechargeList();
        $where = new Where();
        if($type){
            $where['status']=$type;
        }
        $list = $recharge->getList($where,'create_time desc',$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 内容是否收藏
     * @method is_article_collection
     * @param int $id 内容ID true
     * @route('v1/user/is_article_collection','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function is_article_collection($id){
        $info = (new \app\first\model\Article())->find($id);
        if(!$info){
            return $this->sendError(0, '内容不存在');
        }
        $col = new Collection();
        $result = $col->where('mid','eq',self::$uid)
            ->where('aid','eq',$id)
            ->count('1');
        $yelp = (new ArticleLove())->isYelp(self::$uid,$id);
        $data = [
            'collection'=>$result,
            'yelp'=>$yelp
        ];
        return $this->sendSuccess($data);
    }

    /**
     * @title 梦想加评论
     * @method comments
     * @param int $id 文章id true
     * @param string $content 评论内容 true  不超过255字符
     * @param int $flow_id 跟评id false
     * @route('v1/user/comments','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function comments($id=0,$content='',$flow_id=0){
        $arc = new DreamComments();
        $member = $arc->getMemberByFlowId($flow_id);
        if($member['id'] == self::$uid){
            $flow_id = 0;
        }
        $result = $arc->comments($id,$content,self::$uid,$flow_id,$out);
        if(!$result){
            return $this->sendError(0, $out);
        }
        return $this->sendSuccess('','评论成功');
    }

    /**
     * @title 我的梦想加评论
     * @method my_comments
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_comments','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_comments($order='desc',$limit=10){
        $notice = new DreamComments();
        $where[] = ['uid','eq',self::$uid];
        $list = $notice->getList($where,"create_time {$order}",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 我的梦想加跟帖
     * @method my_comments_flow
     * @param int $id 梦想加id true
     * @param string $order 排序 false desc asc正序,desc倒序
     * @route('v1/user/my_comments_flow','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function my_comments_flow($id = 0,$order='desc'){
        $notice = new DreamComments();
        if($id){
            $where[] = ['aid','eq',$id];
        }
        $where[] = ['uid','eq',self::$uid];
        $list = $notice->getAllList($where,"create_time {$order}")->toArray();
//        $_list = Category::unlimitedForLevel2($list,0,'children','flow_id');
        return $this->sendSuccess($list);
    }

    /**
     * @title 点评
     * @method yelp
     * @param int $aid 文章id true
     * @param int $type 类型 type 1 1赞,2踩
     * @route('v1/user/yelp','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function yelp($aid=0,$type=1){
        $yelp = new ArticleLove();
        $result = $yelp->addNew($aid,self::$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 取消点评
     * @method del_yelp
     * @param int $aid 文章id true
     * @param int $type 类型 type 1 1赞,2踩
     * @route('v1/user/del_yelp','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del_yelp($aid=0,$type=1){
        $yelp = new ArticleLove();
        $result = $yelp->delYelp(self::$uid,$aid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','取消成功');
    }
    /**
     * @title 我的点评
     * @method my_yelp
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_yelp','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_yelp($order='desc',$limit=10){
        $yelp = new ArticleLove();
        $where[] = ['uid','eq',self::$uid];
        $list = $yelp->getList($where,"create_time $order",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 发布梦想加
     * @method publish_dream
     * @param string $title 标题 false
     * @param string $image 图片 false  多个使用','隔开
     * @param string $content 内容 true
     * @param string $location 坐标 true
     * @param string $address 地址 true
     * @param string $city_id 城市代码 true
     * @route('v1/user/publish_dream','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function publish_dream($title='',$image='',$content='',$location='',$address='',$city_id=''){
        $dream = new \app\first\model\Dream();
        $result = $dream->addNew(self::$uid,$title,$image,$content,$location,$address,$city_id,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','发布成功');
    }

    /**
     * @title 我梦想加发布
     * @method my_publish_dream
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_publish_dream','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_publish_dream($order='desc',$limit=10){
        $yelp = new \app\first\model\Dream();
        $where[] = ['mid','eq',self::$uid];
        $list = $yelp->getList($where,"create_time $order",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 梦想加点评
     * @method dream
     * @param int $aid 梦想加id true
     * @param int $type 类型 type 1 1赞,2踩
     * @route('v1/user/dream','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function dream($aid=0,$type=1){
        $yelp = new DreamLove();
        $result = $yelp->addNew($aid,self::$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 取消梦想加点评
     * @method del_dream
     * @param int $aid 文章id true
     * @param int $type 类型 type 1 1赞,2踩
     * @route('v1/user/del_dream','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function del_dream($aid=0,$type=1){
        $yelp = new DreamLove();
        $result = $yelp->delYelp(self::$uid,$aid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','取消成功');
    }
    /**
     * @title 我的梦想加点评
     * @method my_dream
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_dream','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_dream($order='desc',$limit=10){
        $yelp = new DreamLove();
        $where[] = ['uid','eq',self::$uid];
        $list = $yelp->getList($where,"create_time $order",$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 记录浏览记录
     * @method add_my_view_log
     * @param int $aid 文章id true
     * @param int $type 类型 true 1 类型:1类目,2特惠,3快讯,4圈子
     * @route('v1/user/add_my_view_log','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function add_my_view_log($aid=0,$type=1){
        $view = new ArticleViewLog();
        $result = $view->addNew($aid,self::$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }
    /**
     * @title 我的浏览记录
     * @method my_view_log
     * @param int $type 类型 false 0 类型:0全部,1类目,2特惠,3快讯,4圈子
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_view_log','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_view_log($type=0,$order='desc',$limit=10){
        $view = new ArticleViewLog();
        $where[] = ['uid','eq',self::$uid];
        if($type){
            $where[]=['type','eq',$type];
        }
        $list = $view->getList($where,"create_time $order",$limit);
        return $this->sendSuccess($list);
    }
    /**
     * @title 记录拨打记录
     * @method add_my_call_log
     * @param int $aid 文章id true
     * @param int $type 类型 true 1 类型:1类目,2特惠,3快讯,4圈子
     * @route('v1/user/add_my_view_log','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function add_my_call_log($aid,$type=1) {
        $view = new \app\first\model\ArticleCallLog();
        $result = $view->addNew($aid,self::$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }
    /**
     * @title 我的拨打记录
     * @method my_call_log
     * @param int $type 类型 false 0 类型:0全部,1类目,2特惠,3快讯,4圈子
     * @param string $order 排序 false desc desc,asc
     * @param int $limit 条数 false 10
     * @param int $page 页数 false 1
     * @route('v1/user/my_view_log','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function my_call_log($type=0,$order='desc',$limit=10){
        $view = new \app\first\model\ArticleCallLog();
        $where[] = ['uid','eq',self::$uid];
        if($type){
            $where[]=['type','eq',$type];
        }
        $list = $view->getList($where,"create_time $order",$limit);
        return $this->sendSuccess($list);
    }

    /*
    public function get_alipay_user1($access_token=''){
        $keyPair = \Alipay\Key\AlipayKeyPair::create(
            file_get_contents('../data/alipay_cert/private_key.pem'),
            file_get_contents('../data/alipay_cert/public_key.pem')
        );
        $aop = new AopClient(Config::get('alipay.app_id'),$keyPair);
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new AlipayUserInfoAuthRequest();
        $params = [
            'scopes'=> 'auth_base',
            'state'=> 'init'
        ];
        $request->setBizContent(json_encode($params));
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $this->sendSuccess($result->$responseNode);
    }*/

    /**
     * @title 支付宝会员授权信息查询、
     * @method get_alipay_user
     * @param string $access_token auth_token true
     * @route('v1/user/get_alipay_user','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function get_alipay_user($access_token=''){
        $keyPair = \Alipay\Key\AlipayKeyPair::create(
            file_get_contents('../data/alipay_cert/private_key.pem'),
            file_get_contents('../data/alipay_cert/public_key.pem')
        );
        $aop = new AopClient(Config::get('alipay.app_id'),$keyPair);
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='utf-8';
        $aop->format='json';
        $request = new AlipayUserInfoShareRequest();
        $request->setAuthToken($access_token);
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $this->sendSuccess($result->$responseNode);
    }

    /**
     * @title 升级vip
     * @method upgrade_vip
     * @param int $id 分享内容id true
     * @route('v1/user/upgrade_vip','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function upgrade_vip($id=0){
        $member = new Member();
        $user = $member->find(self::$uid);
        if(!$user){
            return $this->sendError(0,'没有登陆');
        }
        if($user['is_vip']==2){
            return $this->sendError(0,'已是vip');
        }
        if(!$id){
            return $this->sendError(0,'缺少参数');
        }
        $article = new \app\first\model\Article();
        $info = $article->find($id);
        if(!$info){
            return $this->sendError(0,'分享内容不存在');
        }
        Db::startTrans();;
        $user->is_vip = 2;
        $result = $user->save();
        $s = new UservipLog();
        $date = time();
        $_date = date('Y-m-d H:i:s',$date);
        $result1 = $s->save([
            'uid'=>self::$uid,
            'content'=> "于{$_date},分享【{$info['title']}(ID:{$info['id']})】升级未VIP",
            'create_time'=> $date
        ]);
        if($result && $result1){
            Db::commit();
            return $this->sendSuccess('','升级vip成功');
        }else{
            Db::rollback();
            return $this->sendError(0,'升级vip失败');
        }
    }
}
